package com.bstek.uflo.form.action.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.bstek.dorado.view.View;
import com.bstek.dorado.web.DoradoContext;
import com.bstek.uflo.client.service.TaskClient;
import com.bstek.uflo.form.Constants;
import com.bstek.uflo.form.action.FormData;
import com.bstek.uflo.model.ProcessDefinition;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.process.node.FormElement;
import com.bstek.uflo.process.node.TaskNode;
import com.bstek.uflo.service.ProcessService;
import com.bstek.uflo.service.TaskOpinion;

/**
 * @author Jacky.gao
 * @since 2013年11月12日
 */
@Component("uflo.form.completeTaskAndSaveFormDataAction")
public class CompleteTaskAndSaveFormDataAction extends SaveFormDataAction {
	@Autowired
	@Qualifier(TaskClient.BEAN_ID)
	private TaskClient taskClient;
	
	@Autowired
	@Qualifier(ProcessService.BEAN_ID)
	private ProcessService processService;
	
	public String getName() {
		return "保存并完成任务";
	}

	public String getIcon() {
		return "url(skin>common/icons.gif) -160px -20px";
	}

	public void onViewInit(View view) {
	}

	public void execute(FormData formData,Session session) {
		super.saveFormData(formData, session);
		long taskId=formData.getTaskId();
		Map<String,Object> processData=formData.getProcessData();
		taskClient.start(taskId);
		if(processData!=null && processData.size()>0){
			String taskOpinion=(String)processData.get(Constants.TASK_OPINION);
			processData.remove(Constants.TASK_OPINION);
			buildTaskVariables(processData,taskId,formData);
			Map<String,Object> variables=new HashMap<String,Object>();
			for(String key:processData.keySet()){
				if(processData.get(key)==null){
					continue;
				}
				variables.put(key, processData.get(key));
			}
			if(variables.size()>0){
				if(StringUtils.isEmpty(taskOpinion)){
					taskClient.complete(taskId, variables);				
				}else{
					TaskOpinion opinion=new TaskOpinion(taskOpinion);
					taskClient.complete(taskId, variables,opinion);				
				}
			}else{
				if(StringUtils.isEmpty(taskOpinion)){
					taskClient.complete(taskId);				
				}else{
					TaskOpinion opinion=new TaskOpinion(taskOpinion);
					taskClient.complete(taskId,opinion);				
				}
			}
		}else{
			taskClient.complete(taskId);
		}
	}
	
	private void buildTaskVariables(Map<String,Object> variables,long taskId,FormData formData){
		Task task=taskClient.getTask(taskId);
		ProcessDefinition process=processService.getProcessById(task.getProcessId());
		TaskNode taskNode=(TaskNode)process.getNode(task.getNodeName());
		List<FormElement> elements=taskNode.getFormElements();
		if(elements==null || elements.size()==0)return;
		Map<String,Object> dataMap=formData.getMasterTable();
		for(FormElement element:elements){
			for(String fieldName:dataMap.keySet()){
				if(fieldName.equals(element.getName())){
					variables.put(fieldName, dataMap.get(fieldName));
					break;
				}
			}
		}
	}

	public String getClickScript(String actionId) {
		StringBuffer sb=new StringBuffer();
		sb.append("dorado.MessageBox.confirm(\"真的要保存数据并完成当前任务吗？\",function(){\n");
		sb.append("view.id(\""+actionId+"\").execute(function(){\n");
		sb.append("dorado.MessageBox.alert(\"操作成功.\");\n");
		sb.append("if(window.parent && window.parent.closeProcessDialog){\n");
		sb.append("window.parent.closeProcessDialog("+DoradoContext.getCurrent().getRequest().getParameter("type")+");\n");
		sb.append("}\n");
		sb.append("});\n");
		sb.append("});\n");
		sb.append("");
		return sb.toString();
	}
}
